package com.yxh.lib_base.ext.recycler.div

import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.OrientationHelper
import androidx.recyclerview.widget.RecyclerView

class ItemDecorationSpaces : RecyclerView.ItemDecoration {
    private var _spaceH: Int = 0
    private var _spaceV: Int = 0
    var _marginStart = 0 //开始边距，根据根据方向判断加载左边还是上边
    var _isV = true //默认纵向布局

    constructor(space_h: Int, space_v: Int, startSpace: Int = 0) {
        this._spaceH = space_h
        this._spaceV = space_v
        //横向布局就是左  纵向就是上  边缘间距
        this._marginStart = startSpace
    }

    /**设置方向*/
    fun asOrientation(@RecyclerView.Orientation orientation: Int): ItemDecorationSpaces {
        this._isV = orientation == OrientationHelper.VERTICAL
        return this
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        outRect.right = _spaceH
        outRect.bottom = _spaceV
        val position = parent.getChildAdapterPosition(view)
        if (_isV) {
            if (position == 0) {
                outRect.top = _marginStart
            }
            outRect.left = _spaceH
        } else {
            if (position == 0) {
                outRect.left = _marginStart
            }
            outRect.top = _spaceV
        }
    }
}